/*
 Copyright (c) 2008 TPSi
 Alex Iskander
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
 */


#ifndef BINDABLE_BINDABLE_SELECTOR_H_
#define BINDABLE_BINDABLE_SELECTOR_H_

namespace Bind
{
	class Bindable;
	class Bindable;
	
	/**
	 \class Selector
	 \brief Abstract. Manages selecting properties from BindableObject objects.
	 
	 Selectors are used to fetch objects from BindableObject objects. They are the
	 values of BindableProperties' bindTo data member, and are the methodology
	 used to auto-bind one item with another.
	 
	 \par Example
	 @code
	 RGBColor rgb(255, 0, 0);
	 //rgb has properties named "red", "green", and "blue"
	 
	 MyColor color;
	 color.red.bindTo(new NameSelector("red"));
	 color.green.bindTo(new NameSelector("green"));
	 color.blue.bindTo(new NameSelector("blue"));
	 
	 color = rgb; //automatically maps
	 
	 MyComposite comp; //has an RGBColor in it.
	 
	 color.red.bindTo(new NameSelector("color")).next(new NameSelector("red"));
	 color.green.bindTo(new NameSelector("color")).next(new NameSelector("green"));
	 color.blue.bindTo(new NameSelector("color")).next(new NameSelector("blue"));
	 
	 @endcode
	 
	 \par Deriving
	 There is one function which derived classes \e must implement:
	 run(BindableObject *bindable);
	 
	 Run is the function which runs the active part of the binding. The get()
	 function returns the result of run, but only after processing sub-selectors.
	 
	 The processing of sub-selectors should usually not be changed by derived
	 classes, so it was separated from the actual selector processing.
	 
	 \par Types Of Selectors
	 While the main purpose of selectors is to select, they can also be used to
	 do a variety of tasks.
	 
	 For instance, there is a selector called AsString, which (if the selected
	 property supports it) will return a string representation of the provided
	 property.
	 */
	class Selector
	{
	public:
		/**
		 \fn Selector();
		 \brief Constructor.
		 */
		Selector();
		
		/**
		 \fn get(Bindable *object);
		 \brief Runs the selector and returns the result.
		 
		 If there are any sub-selectors, these are run as well.
		 */
		virtual Bindable *get(Bindable *object);
		
		/**
		 \fn next();
		 \brief Returns the next item in the selector chain. NULL if none.
		 */
		virtual Selector *next();
		
		/**
		 \fn next(Selector *selector, bool autoDelete = true);
		 \brief Adds a selector to the selector chain.
		 
		 \param selector The selector to be added. If NULL, the current selector
		 is removed (and auto-deleted if necessary).
		 
		 \param autoDelete Determines whether the provided selector should be
		 automatically deallocated.
		 */
		virtual Selector *next(Selector *selector, bool autoDelete = false);
		
		/**
		 \fn ~Selector();
		 \brief Destructor.
		 */
		~Selector();
		
	protected:
		/**
		 \fn run(Bindable *object);
		 \brief Must be implemented by derived classes. Processes the selector.
		 
		 Should return the result (NULL if not found).
		 */
		virtual Bindable *run(Bindable *object) = 0;
		
		Selector *_next;
		bool autoDeleteNext;
	};
	
}

#endif
